#!/bin/bash

#=================================================================================
nginx_download_path="http://nginx.org/download/nginx-1.12.0.tar.gz"
nginx_install_dir="/usr/local/nginx"
nginx_name="nginx-1.12.0.tar.gz"

mysql_download_path="http://mirrors.sohu.com/mysql/MySQL-5.6/mysql-5.6.35.tar.gz"
mysql_install_dir="/usr/local/mysql"
mysql_name="mysql-5.6.35.tar.gz"

php_download_path="http://jp2.php.net/distributions/php-5.5.38.tar.gz"
php_install_path="/usr/local/php"
php_name="php-5.5.38.tar.gz"
#=================================================================================

dir=/data

[ -f /etc/init.d/functions ] && source /etc/init.d/functions || exit 1

# create file download dir 
create_dir(){
    if [ ! -e $dir ]
    then 
    mkdir $dir
    else
        echo "file download dir is exists"
    fi
}

# install nginx 1.12.0
install_nginx(){
    #yum install epel.repo
    wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-6.repo

    #yum install package
    yum install -y pcre pcre-devel openssl openssl-devel gcc make gcc-c++

    #download nginx software
    [ -e $dir ] && cd $dir
    wget $nginx_download_path
    if [ -f $nginx_name ]
    then 
        echo "nginx download success"
    fi

    #extract nginx software
    tar zxf $nginx_name && cd $dir/nginx-1.12.0
    #add ningx user
    useradd nginx -s /sbin/nologin -M

    #install nginx
    ./configure --user=nginx --group=nginx --prefix=/usr/local/nginx --with-http_stub_status_module --with-http_ssl_module

    if [ $? -eq 0 ]
    then 
    make && make install 
    if [ $? -eq 0 ]
    then 
        echo "nginx install successful!"
    fi
    fi     
}

# start nginx
start_nginx(){
    # check syntax
    $nginx_install_dir/sbin/nginx -t 

    # start
    if [ $? -eq 0 ]
    then 
        $nginx_install_dir/sbin/nginx 
    if [ $(netstat -antplu|grep 80|wc -l) -eq 1 ]
    then
        action "nginx start success..." /bin/true
    else
        echo "nginx starting fail,please check the service!"
    fi
    fi
}

# install mysql 5.6.35
install_mysql(){
    # yum install some package
    yum -y install make gcc-c++ cmake bison-devel ncurses-devel

    #download mysql software
    [ -e $dir ] && cd $dir
    wget $mysql_download_path
    if [ -f $mysql_name ]
    then 
    echo "mysql download success"
    fi

    # extract mysql 
    echo "just wait a moment,mysql is Extracting..."
    tar zxf $mysql_name && cd $dir/mysql-5.6.35

    # install mysql
    cmake \
    -DCMAKEINSTALLPREFIX=/usr/local/mysql \
    -DMYSQLDATADIR=/usr/local/mysql/data \
    -DSYSCONFDIR=/etc \
    -DWITHMYISAMSTORAGEENGINE=1 \
    -DWITHINNOBASESTORAGEENGINE=1 \
    -DWITHMEMORYSTORAGEENGINE=1 \
    -DWITHREADLINE=1 \
    -DMYSQLUNIXADDR=/var/lib/mysql/mysql.sock \
    -DMYSQLTCPPORT=3306 \
    -DENABLEDLOCALINFILE=1 \
    -DWITHPARTITIONSTORAGEENGINE=1 \
    -DEXTRACHARSETS=all \
    -DDEFAULTCHARSET=utf8 \
    -DDEFAULTCOLLATION=utf8general_ci

    if [ $? -eq 0 ]
    then
        make && make install
        if [ $? -eq 0 ]
        then
            echo "mysql install successful!"
        fi
    fi

    # create user and group
    groupadd mysql
    useradd -g mysql mysql

    # grant to mysql
    chown -R mysql:mysql /usr/local/mysql

    # init 
    cd $mysql_install_dir
    scripts/mysql_install_db --basedir=/usr/local/mysql --datadir=/usr/local/mysql/data --user=mysql
    mv /etc/my.cnf /etc/my.cnfbak

    cp support-files/mysql.server /etc/init.d/mysql


    # add to boot auto launch
    chkconfig --add mysql
    chkconfig mysql on

    # add to PATH
    echo "export PATH=$PATH:/usr/local/mysql/bin/" >>/etc/profile
    source /etc/profile
}

# start mysql server 
start_mysql(){
    # start
    /etc/init.d/mysql start
    if [ $(netstat -antplu|grep 3306|wc -l) -eq 1 ]
    then
        action "mysql start success..." /bin/true
    else
    echo "mysql starting fail,please check the service!"
    fi
}

# install php5.5.38
install_php(){
    # yum isntall some package
    yum install zlib-devel openssl-devel openssl libxml2-devel libjpeg-devel libjpeg-turbo-devel libiconv-devel freetype-devel libpng-devel gd-devel libcurl-devel libxslt-devel libxslt-devel libmcrypt-devel mcrypt mhash -y

    # download php
    [ -e $dir ] && cd $dir
    wget $php_download_path
    if [ -f $php_name ]
    then
        echo "php download success"
    fi

    # extract php software
    tar zxf $php_name && cd php-5.5.38

    #install php
    ./configure --prefix=/usr/local/php --with-mysql=/usr/local/mysql --with-iconv-dir=/usr/lcoal/libiconv --with-freetype-dir --with-jpeg-dir --with-png-dir --with-zlib --with-libxml-dir=/usr --enable-xml --disable-rpath --enable-bcmath --enable-shmop --enable-sysvsem --enable-inline-optimization --with-curl --with-curlwrappers --enable-mbregex --enable-fpm --enable-mbstring --with-mcrypt --with-gd --enable-gd-native-ttf --with-openssl --with-mhash --enable-pcntl --enable-sockets --with-xmlrpc --enable-zip --enable-soap --enable-short-tags --enable-zend-multibyte --enable-static --with-xsl --with-xsl --with-fpm-user=nginx --with-fpm-user=nginx --enable-ftp --enable-opcache=no

    if [ $? -eq 0 ]
    then
    ln -s /usr/local/mysql/lib/libmysqlclient.so.18 /usr/lib64/ && touch ext/phar/phar.phar

        make && make install
        if [ $? -eq 0 ]
        then
            echo "php install successful!"
        fi
    fi


    # copy php.ini
    cp php.ini-development /etc/php.ini

    # copy php-fpm
    cp /usr/local/php/etc/php-fpm.conf.default /usr/local/php/etc/php-fpm.conf
    cp sapi/fpm/init.d.php-fpm /etc/init.d/php-fpm 

    chmod +x /etc/init.d/php-fpm

    # add to PATH
    echo "export PATH=$PATH:/usr/local/php/bin/" >>/etc/profile
    source /etc/profile  

    # add to boot auto launch
    chkconfig --add php-fpm
    chkconfig php-fpm  on 
}

# start php-fpm
start_php(){
    # start
    /etc/init.d/php-fpm start
    if [ $(netstat -antplu|grep 9000|wc -l) -eq 1 ]
    then
    action "php-fpm start success..."  /bin/true
    else
    echo "php-fpm starting fail,please check the service!"
    fi
}

main(){
    # create dir
    create_dir

    # install nginx
    install_nginx

    # install mysql
    install_mysql

    # install php
    install_php

    # start
    start_nginx
    sleep 2
    start_mysql
    sleep 2
    start_php
}
main